package com.yunding.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldStrategy;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.MybatisXMLLanguageDriver;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.core.config.GlobalConfig.DbConfig;
import com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.type.JdbcType;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;


@Configuration
@MapperScan(basePackages = "com.yunding.OA.mapper", sqlSessionFactoryRef = "otherSqlSessionFactory")
public class MybatisDbOtherConfig {

    @Bean(name = "otherDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.other")
    public DataSource dataSource() {
//        return DataSourceBuilder.create().build();
        return DataSourceBuilder.create()
                .type(HikariDataSource.class)
                .driverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver")
                .url("jdbc:sqlserver://101.207.141.143:1999;DatabaseName=OA")
                .username("sa")
                .password("system123456")
                .build();
    }


    @Bean(name = "otherTransactionManager")
    public DataSourceTransactionManager transactionManager(@Qualifier("otherDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);

    }

    @Bean(name = "otherSqlSessionFactory")
    public SqlSessionFactory basicSqlSessionFactory(@Qualifier("otherDataSource") DataSource basicDataSource) throws Exception {
//        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
//        factoryBean.setDataSource(basicDataSource);
//        factoryBean.setMapperLocations(
//                new PathMatchingResourcePatternResolver().getResources("classpath*:mapperOA/*.xml"));
//        return factoryBean.getObject();

        MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
        sqlSessionFactory.setDataSource(basicDataSource);
        MybatisConfiguration configuration = new MybatisConfiguration();
        configuration.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
        configuration.setJdbcTypeForNull(JdbcType.NULL);
        configuration.setCallSettersOnNulls(true);
        sqlSessionFactory.setConfiguration(configuration);

        sqlSessionFactory.setTypeAliasesPackage("com.yunding.OA.pojo");
        GlobalConfig globalConfig = new GlobalConfig();
        DbConfig dbConfig = new DbConfig();
        dbConfig.setDbType(DbType.SQL_SERVER);
        dbConfig.setIdType(IdType.ID_WORKER_STR);
        dbConfig.setFieldStrategy(FieldStrategy.NOT_EMPTY);
        dbConfig.setTableUnderline(true);
        globalConfig.setDbConfig(dbConfig);
        globalConfig.setWorkerId(1l);
        globalConfig.setDatacenterId(1l);
        sqlSessionFactory.setGlobalConfig(globalConfig);

        sqlSessionFactory
                .setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mybatisoa/*.xml"));
//		sqlSessionFactory.setPlugins(new Interceptor[] {
////                    new PaginationInterceptor(),//<!-- 分页插件配置 -->
        new PerformanceInterceptor();// <!-- 性能拦截器，兼打印sql，不建议生产环境配置-->
////                    new OptimisticLockerInterceptor()//<!-- 乐观锁插件 -->
//		});
        return sqlSessionFactory.getObject();

    }

    @Bean(name = "otherSqlSessionTemplate")
    public SqlSessionTemplate testSqlSessionTemplate(
            @Qualifier("otherSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
